﻿using System;
using System.Collections.ObjectModel;
using WPF数据采集系统.Base;
using WPF数据采集系统.BLL;
using WPF数据采集系统.Model;

namespace WPF数据采集系统.ViewModel
{
    public class PlcViewModel : NotifyPropertyBase
    {
        public MainBLL mb = new MainBLL();
        public PlcViewModel()
        {
            this.LoadPlcInfo();
            this.LoadPlcMonitorInfo();
        }
        public PlcViewModel(string ip)
        {
            this.LoadPlcInfo();
            this.LoadPlcMonitorInfo();
        }
        public bool LoadPlcInfo()
        {
            plcInfoList = new ObservableCollection<PlcModel>();
            var plcinfo = mb.LoadPlcInfo();
            if (plcinfo.State)
            {
                foreach (PlcModel pm in plcinfo.Data)
                {
                    plcInfoList.Add(pm);
                }
                return true;
            }
            else
            {
                CustomMsgBox.ShowDialog(plcinfo.Message);
                return false;
            }
        }
        public bool LoadPlcMonitorInfo()
        {
            plcMonitorInfoList = new ObservableCollection<PlcMonitorInfo>();
            var plcMonitorInfo = mb.LoadPlcMonitorInfo();
            if (plcMonitorInfo.State)
            {
                foreach (PlcMonitorInfo pmi in plcMonitorInfo.Data)
                {
                    plcMonitorInfoList.Add(pmi);
                }
                return true;
            }
            else
            {
                CustomMsgBox.ShowDialog(plcMonitorInfo.Message);
                return false;
            }
        }
        private PlcModel _pm;
        //private PlcModel _pm;
        public PlcModel pm
        {
            get { return _pm; }
            set
            {
                _pm = value;
                this.RaisePropertyChanged();
            }
        }
        private ObservableCollection<PlcModel> _plcInfoList;
        public ObservableCollection<PlcModel> plcInfoList
        {
            get { return _plcInfoList; }
            set
            {
                _plcInfoList = value;
                this.RaisePropertyChanged();
            }
        }
        private ObservableCollection<PlcMonitorInfo> _plcMonitorInfoList;
        public ObservableCollection<PlcMonitorInfo> plcMonitorInfoList
        {
            get { return _plcMonitorInfoList; }
            set
            {
                _plcMonitorInfoList = value;
                this.RaisePropertyChanged();
            }
        }
        public bool SavePlcData(PlcModel pm)
        {
            try
            {
                return mb.SavePlcData(pm);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
    }
}
